package com.luguotao.lease.web.admin.service.impl;

import com.luguotao.lease.common.constant.RedisConstant;
import com.luguotao.lease.common.exception.LeaseException;
import com.luguotao.lease.common.result.ResultCodeEnum;
import com.luguotao.lease.common.utils.JwtUtil;
import com.luguotao.lease.model.entity.SystemUser;
import com.luguotao.lease.model.enums.BaseStatus;
import com.luguotao.lease.web.admin.mapper.SystemUserMapper;
import com.luguotao.lease.web.admin.service.LoginService;
import com.luguotao.lease.web.admin.vo.login.CaptchaVo;
import com.luguotao.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.util.StringUtils;
import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    RedisTemplate  redisTemplate;
    @Autowired
    SystemUserMapper systemUserMapper;

    @Override
    public CaptchaVo getCaptcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(138,48,4);
        String verCode = specCaptcha.text().toLowerCase();
        String image = specCaptcha.toBase64();

        //获取到的验证码verCode，给它加点前缀
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString();
//放入redis中保存，设置过期时间一分钟
        redisTemplate.opsForValue().set(key,verCode,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

        CaptchaVo captchaVo = new CaptchaVo(image,key);
        return captchaVo;
    }

    @Override
    public String login(LoginVo loginVo) {
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        //key
        String captchaKey = loginVo.getCaptchaKey();
        //验证码
        String captchaCode = loginVo.getCaptchaCode();

        //验证码不能为空
        if (!StringUtils.hasText(captchaCode)){
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_CODE_EMPTY);
        }
        //获取redis中的验证码
        Object redisCode = redisTemplate.opsForValue().get(captchaKey);
        //redis中的验证码过期会自动删除，所以判断是否为空
        if (redisCode == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //没过期，验证码与redis中的验证码进行比对
        if (!captchaCode.equals(redisCode)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }

        //根据用户名到数据库中查询信息
        SystemUser systemUser = systemUserMapper.selectOne(new LambdaUpdateWrapper<SystemUser>().eq(SystemUser::getUsername, username));
        //判断是否存在
        if (systemUser == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //判断账户状态
        if(systemUser.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        System.out.println(DigestUtils.md5Hex(password));
        //比较登录密码,算法
        if (!DigestUtils.md5Hex(password).equals(systemUser.getPassword())){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        //给登录成功的用户创建token
        String token = JwtUtil.creatToken(systemUser.getId(), systemUser.getUsername());

        System.out.println("______________登录成功_______________");
        return token;

    }




















}
